package com.zhiyou.servlet;

import com.zhiyou.model.Contract;
import com.zhiyou.model.House;
import com.zhiyou.service.ContractService;
import com.zhiyou.service.impl.ContractServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class ContractUpdateServlet extends HttpServlet {
    private ContractService cS=new ContractServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        System.out.println("更新执行");
        String idStr = req.getParameter("cid");
        int cid=Integer.parseInt(idStr);
        Contract contract = cS.selectById(cid);
        req.getSession().setAttribute("contract",contract);
        //接受参数判断是更新还是详情
        String detail = req.getParameter("detail");
        if (detail!=null){
            resp.sendRedirect(req.getContextPath()+"/view/contract/detail.jsp");
        }else{
            resp.sendRedirect(req.getContextPath()+"/view/contract/edit.jsp");
        }


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        System.out.println("更新成功");
        int cid = Integer.parseInt(req.getParameter("cid"));
        String cnum =req.getParameter("cnum");
        int chid = Integer.parseInt(req.getParameter("chid"));
        int clid = Integer.parseInt(req.getParameter("clid"));
        String ctime = req.getParameter("ctime");
        String cstartTime = req.getParameter("cstartTime");
        String cendTime = req.getParameter("cendTime");
        String ctotalMoney = req.getParameter("ctotalMoney");
        int cpayType = Integer.parseInt(req.getParameter("cpayType"));

        //判断参数数据是否符合更新条件

        cS.updateNew(cid,cnum,chid,clid,ctime,cstartTime,cendTime,ctotalMoney,cpayType);
        resp.sendRedirect(req.getContextPath()+"/user/contract?pageNo="+req.getParameter("pageNo"));
    }
}
